home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / E-Z Progress Bar 1.0a / main.cp < prev    next >
Text File  |  1995-03-15  |  4KB  |  122 lines

  1. #include "ProgressClass.h"
  2.  
  3. void InitToolbox(void);
  4. void    GetStringX(Str255 s1, Str255 s2, short i);
  5.  
  6. void InitToolbox()
  7. {
  8.     InitGraf((Ptr) &qd.thePort);
  9.     InitFonts();
  10.     InitWindows();
  11.     InitMenus();
  12.     FlushEvents(everyEvent,0);
  13.     TEInit();
  14.     InitDialogs(0L);
  15.     InitCursor();
  16. }
  17.  
  18. main()
  19. {
  20.     InitToolbox();
  21.     
  22.     ProgressClass    myBar(progressDefaults-fineChisel);    //    options
  23.  
  24.     //    An instance of ProgressClass is created.  All default options except the
  25.     //    fineChisel effect are turned on.
  26.         
  27.     Str255    theString;
  28.     short    i;
  29.     Boolean    cancel=0, deactivate;
  30.     WindowPtr    updateWindow;
  31.     
  32.     //    The following call will set a minimum and a maximum bar value of 0 and 20,
  33.     //    respectively, so that when a number between that range is passed via "SetBar"
  34.     //    the proper bar percentage is automatically displayed.  SetTexts will set the
  35.     //    window title to "Progress Window" and the parameter text above the bar to
  36.     //    "Progress Bar".  Later, SetBar calls with a string in the second argument
  37.     //    will change this parameter text.
  38.     //    Finally, ShowBarWindow sets the port to the progress bar window and displays
  39.     //    it.  The class will always set the port back to the original port when it 
  40.     //    finishes with a task (or at least it should - let me know if you have problems).
  41.     
  42.     myBar.SetMinMax(0, 20);
  43.     myBar.SetTexts("\pProgress Window", "\pProgress Bar");
  44.     myBar.ShowBarWindow();
  45.     
  46.     for (i=0; i<=20 && !cancel; i++) {
  47.         if (!myBar.ReceivedEvent(&cancel, &deactivate, &updateWindow)) {
  48.         
  49.             //    If no event concerning you has been received, continue with
  50.             //    your task here.  Here, a number is converted to a string and the new
  51.             //    number is posted along with the new update to the progress bar in
  52.             //    the SetBar call (sets the bar to a new position)
  53.     
  54.             GetStringX("\pFirst Round ", theString, i);
  55.             myBar.SetBar(i, theString);
  56.             Delay(10, nil);
  57.         }
  58.         else {
  59.             
  60.             //    An event has been received.  Examine the variables passed in the
  61.             //    function parameters to find out what event has transpired.
  62.             //    If the user has cancelled, the first paramater will be true, while
  63.             //    the others will be false (or nil). When your window is deactivated,
  64.             //    deactivate will return true and the window's pointer will be in
  65.             //    the third paramater.  If you receive an update event,
  66.             //    the window to be updated will be returned in the third parameter.
  67.         
  68.             if (cancel)
  69.                 ;            //    User cancelled, cease and desist
  70.             else if (deactivate)
  71.                 ;            //    Your window is in the background;
  72.                             //    do whatever is necessary to be in backgound
  73.             else if (updateWindow)
  74.                 ;            //    Update this window
  75.         }                    
  76.     }
  77.  
  78.     
  79.     //    The next call will create a custom control, using the CNTL resource 128
  80.     //    If it is not found, the default cancel button is used
  81.     //    The bar height is also set to 15 pixels
  82.     
  83.     myBar.SetOptions(progressDefaults);        //    This will turn on all defaults
  84.     myBar.SetControlID(128);
  85.     myBar.SetBarHeight(15);
  86.  
  87.     cancel=0;
  88.     
  89.     for (i=0; i<=20 && !cancel; i++) {
  90.         if (!myBar.ReceivedEvent(&cancel, &deactivate, &updateWindow)) {
  91.             
  92.             //    In this example, we are setting the progress bar via a float variable,
  93.             //    whereas before it was a short.  You can use a float to represent a 
  94.             //    percentage of the task being done, which means this will only
  95.             //    represent numbers in the range of 0.0 to 1.0.  All other values will be
  96.             //    considered zero if less than zero, or one if greater than one.
  97.         
  98.             myBar.SetBar((float) ((float)i/20.0), "\pYour action name here…");
  99.             Delay(10, nil);
  100.         }
  101.         else
  102.             ;            //    examine parameters here as above
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108.  
  109. void    GetStringX(Str255 s1, Str255 s2, short i)
  110. {
  111.     Str255    sNumber;
  112.     
  113.     NumToString(i, sNumber);
  114.     
  115.     s2[0]=s1[0]+sNumber[0];
  116.     
  117.     for (i=1; i<=s1[0]; i++)
  118.         s2[i]=s1[i];
  119.     
  120.     for (i=1; i<=sNumber[0]; i++)
  121.         s2[s1[0]+i]=sNumber[i];
  122. }